package hhm.android.main

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.launcher.ARouter
import hhm.android.base.adapter.HBaseAdapter
import hhm.android.library.utils.GlideUtils
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.*
import siau.android.http.model.DeviceModel
import siau.android.library.KeyString

class MachineAdapter(var context: Context, list: ArrayList<DeviceModel>) : HBaseAdapter<MachineViewHolder, DeviceModel>(list) {
    override fun onBindViewHolder(holder: MachineViewHolder, position: Int) {
        if (position == 0) {
            holder.itemView.setOnClickListener(object : OnMultiClickListener() {
                override fun onMultiClick(v: View?) {
                    if (SBApplication.checkHasChildren()) {
                        ARouter.getInstance().build(MACHINE_ROUTER).navigation()
                    } else {
                        getAddChildDialog(context) { ARouter.getInstance().build(BABY_ROUTE).withInt(KeyString.type, 0).navigation() }
                    }
                }
            })
        } else {
            val bean = list[position - 1]
            val layoutParams = holder.iv.layoutParams
            when (bean.deviceType) {
                1 -> {
                    layoutParams.width = dp2px(context, 66f)
                    layoutParams.height = dp2px(context, 68f)
                }
                2 -> {
                    layoutParams.width = dp2px(context, 15f)
                    layoutParams.height = dp2px(context, 68f)
                }
                3 -> {
                    layoutParams.width = dp2px(context, 29f)
                    layoutParams.height = dp2px(context, 66f)
                }
            }
            holder.iv.layoutParams = layoutParams
            GlideUtils.loadImage(
                context, bean.productImg, holder.iv, when (bean.deviceType) {
                    1 -> {
                        R.mipmap.weight_machine_model
                    }
                    2 -> {
                        R.mipmap.height_machine_model
                    }
                    3 -> {
                        R.mipmap.tooth_brush_machine_model
                    }
                    else -> {
                        R.mipmap.no_data_bg
                    }
                }, when (bean.deviceType) {
                    1 -> {
                        R.mipmap.weight_machine_model
                    }
                    2 -> {
                        R.mipmap.height_machine_model
                    }
                    3 -> {
                        R.mipmap.tooth_brush_machine_model
                    }
                    else -> {
                        R.mipmap.no_data_bg
                    }
                }
            )
            if (TextUtils.isEmpty(bean.deviceName)) {
                holder.tvName.text = bean.productName
            } else {
                holder.tvName.text = bean.deviceName
            }

            holder.itemView.setOnClickListener(object : OnMultiClickListener() {
                override fun onMultiClick(v: View?) {
                    when (bean.deviceType) {
                        1 -> {
                            ARouter.getInstance().build(WEIGHT_MACHINE_ROUTE).withSerializable(KeyString.data, bean).navigation()
                        }
                        2 -> {
                            ARouter.getInstance().build(HEIGHT_MACHINE_ROUTE).withSerializable(KeyString.data, bean).navigation()
                        }
                        3 -> {
                            ARouter.getInstance().build(BLUETOOTH_MACHINE_ROUTE).withSerializable(KeyString.data, bean).navigation()
                        }
                    }
                }
            })
        }
    }

    override fun bindView(parent: ViewGroup, viewType: Int): MachineViewHolder {
        return if (viewType == 1) {
            val v = LayoutInflater.from(context).inflate(R.layout.item_add_machine, parent, false)
            MachineViewHolder(v, viewType)
        } else {
            val v = LayoutInflater.from(context).inflate(R.layout.item_machine_list, parent, false)
            MachineViewHolder(v, viewType)

//            val v = LayoutInflater.from(parent.context).inflate(R.layout.item_three_fragment, parent, false)
//            MachineViewHolder(v, viewType)
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) {
            1
        } else {
            2
        }
    }

    override fun getItemCount(): Int {
        return super.getItemCount() + 1
    }

    override fun bindData(holder: MachineViewHolder, bean: DeviceModel, position: Int) {
    }
}